package com.by.wx.service.impl;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.by.common.util.ThreadLocalDateUtil;
import com.by.wx.dao.PhoneMemberDao;
import com.by.wx.entity.PhoneCode;
import com.by.wx.entity.PhoneMember;
import com.by.wx.entity.PhoneMemberResult;
import com.by.wx.service.PhoneMemberService;

@Service
public class PhoneMemberServiceImpl implements PhoneMemberService {

	public static Log log = LogFactory.getLog(PhoneMemberServiceImpl.class);

	@Autowired
	private PhoneMemberDao phoneMemberDao;

	@Override
	public int updThePhoneMemberByPhone(String openid, String nickname, String phone) {
		if (StringUtils.isEmpty(phone))
			throw new RuntimeException("更新openid的手机号码不能为空");
		return phoneMemberDao.updThePhoneMemberByPhone(openid, nickname, phone);
	}

	@Override
	public int doSaveWxPhoMember(PhoneMember entity) {

		if (entity == null)
			throw new IllegalArgumentException("保存对象不能为空");
		if (StringUtils.isEmpty(entity.getPhone()))
			throw new IllegalArgumentException("保存手机号码不能为空");
		if (StringUtils.isEmpty(entity.getUsername()))
			throw new IllegalArgumentException("保存用户名不能为空");
		PhoneMember pm = new PhoneMember();
		// if ("1".equals(entity.getGender())) {
		// pm.setGender("男");
		// }else if ("2".equals(entity.getGender())) {
		// pm.setGender("女");
		// }else {
		// pm.setGender("未知");
		// }
		if (StringUtils.isEmpty(entity.getIdType())) {
			throw new IllegalArgumentException("保存证件类型不能为空");
		} else if ("0".equals(entity.getIdType())) {
			pm.setIdType("身份证");
		}
		if (entity.getIdNumber().length() < 15 || entity.getIdNumber().length() > 19) {
			throw new IllegalArgumentException("身份证号码格式不正确");
		}
		pm.setIdNumber(entity.getIdNumber());
		pm.setGender(entity.getGender());
		if (!StringUtils.isEmpty(entity.getRegisterWay())) {
			pm.setRegisterWay(entity.getRegisterWay());
		} else {
			pm.setRegisterWay("微信注册");
		}
		pm.setUsername(entity.getUsername());
		pm.setNickname(entity.getNickname());
		pm.setOpenid(entity.getOpenid());
		pm.setPhone(entity.getPhone());
		return phoneMemberDao.doSaveWxPhoMember(pm);
	}

	@Override
	public int doSaveWxMemPhoneCode(PhoneMember entity) {
		if (entity == null)
			throw new IllegalArgumentException("保存对象不能为空");
		if (StringUtils.isEmpty(entity.getPhone()))
			throw new IllegalArgumentException("保存手机号码不能为空");
		if (StringUtils.isEmpty(entity.getUsername()))
			throw new IllegalArgumentException("保存用户名不能为空");
		PhoneMember pm = new PhoneMember();
		if (StringUtils.isEmpty(entity.getIdType())) {
			throw new IllegalArgumentException("保存证件类型不能为空");
		} else if ("0".equals(entity.getIdType())) {
			pm.setIdType("身份证");
		}
		if (entity.getIdNumber().length() < 15 || entity.getIdNumber().length() > 19) {
			throw new IllegalArgumentException("身份证号码格式不正确");
		}
		pm.setIdNumber(entity.getIdNumber());
		pm.setGender(entity.getGender());
		if (!StringUtils.isEmpty(entity.getRegisterWay())) {
			pm.setRegisterWay(entity.getRegisterWay());
		} else {
			pm.setRegisterWay("微信注册");
		}
		// 区别
		if (StringUtils.isEmpty(entity.getCoded())) {
			throw new IllegalArgumentException("手机验证码不能为空！");
		} else {
			pm.setCoded(entity.getCoded());
		}

		pm.setUsername(entity.getUsername());
		pm.setNickname(entity.getNickname());
		pm.setOpenid(entity.getOpenid());
		pm.setPhone(entity.getPhone());
		return phoneMemberDao.doSaveWxPhoMember(pm);
	}

	@Override
	public List<PhoneCode> doFindLasttimeByPhone(String phone, int lim) {
		return phoneMemberDao.doFindLasttimeByPhone(phone, lim);
	}

	@Override
	public List<PhoneCode> updatetimeTest(String phone) {
		List<PhoneCode> lt = phoneMemberDao.doFindLasttimeByPhone(phone, 1);
		try {
			Date ss = lt.get(0).getGenerateTime();
			String ls = ThreadLocalDateUtil.formatDate(ss);
			Timestamp ll = Timestamp.valueOf(ls);
			String la = String.valueOf(ll);

		} catch (ParseException e) {
			e.printStackTrace();
		}
		return lt;
	}

	@Override
	public int doDelUpdateOpenid(String openid) {
		return phoneMemberDao.doDelUpdateOpenid(openid);
	}

	@Override
	public List<PhoneMemberResult> doCheckVerifyRoomJumpOpenid(String openid) {
		return phoneMemberDao.doCheckVerifyRoomJumpOpenid(openid);
	}

}
